import ReactDOM from 'react-dom/client'
import Loading from './loading'

let count = 0
export const showLoading = () => {
  if (count === 0) {
    const loading = document.createElement('div')
    loading.setAttribute('id', 'loading')
    document.body.appendChild(loading) //必须先将dom加入到body
    // console.log('loading:', document.getElementById('loading'))
    ReactDOM.createRoot(loading).render(<Loading />)
  }
  count++
}

export const hideLoading = () => {
  if (count <= 0) return
  count--
  console.log('remove:', document.getElementById('loading'))

  if (count === 0) document.body.removeChild(document.getElementById('loading') as HTMLDivElement)
}
